<?php
/**
 * unit tests for DbResult class, PHP5 version
 */

class TestDbResult extends UnitTestCase {

    function setUp() {

    }

    function testInterface() {
        $sql = 'SELECT * FROM dbresults';
        $result = $this->_getResult($sql);

        $this->assertTrue(
            method_exists($result,'setMysqli')
            , "DbResult should have a setMysqli() method");
        $this->assertTrue(
            method_exists($result,'getSql')
            , "DbResult should have a getSql() method");
        $this->assertTrue(
            method_exists($result,'count')
            , "DbResult should have a count() method");
        $this->assertTrue(
            method_exists($result,'fetchAssoc')
            , "DbResult should have a fetchAssoc() method");
        $this->assertTrue(
            method_exists($result,'fetchCsv')
            , "DbResult should have a fetchCsv() method");
    }

    function testCycle() {
        $sql = 'SELECT * FROM dbresults';
        $result = $this->_getResult($sql);

        $this->assertEqual(
            $result->getSql(), $sql
            , "getSql() should return SQL query");

        $line = $result->fetchAssoc();
        $this->assertIsA(
            $line, 'array'
            , "fetchAssoc() should return an array %s");
    }

    /**
     * builds a result from SQL
     * @param string $sql
     * @return DbResult
     */
    function _getResult($sql) {
        // getting admin's unit tests connection
        $admin = Webappkit::getKit('webappkit.admin');
        $link = $admin->getTestsDbLink();
        if (!$result = $link->getResult($sql)) {
            $this->fail($link->getError());
            $this->skipIf(true
            ,"$sql failed to produce a DbResult");
        }

        return $result;
    }

    /**
     * get a functional mysqli instance
     * @return mysqli
     */
    function _getMysqli() {
        // getting admin's unit tests connection
        $admin = Webappkit::getKit('webappkit.admin');
        $link = $admin->getTestsDbLink();
    }
}